package com.itdr.protal.controller;


import com.itdr.common.Const;
import com.itdr.common.ServerResponse;
import com.itdr.pojo.vo.UserVO;
import com.itdr.protal.service.IPayinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import static org.apache.catalina.manager.Constants.CHARSET;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author AirZhang
 * @since 2020-10-20
 */
@RestController
@RequestMapping("/zfb/")
public class PayinfoController {
    @Autowired
    private IPayinfoService payinfoService;

    @Value("${zfb.returnUrl}")
    private String returnUrl;

    /**
     * 电脑网站支付
     * @param httpSession
     * @param shopName
     * @param orderNo
     * @param totalPrice
     * @return
     */
    @PostMapping("zf")
    public ServerResponse zf(HttpSession httpSession,String shopName, String orderNo, BigDecimal totalPrice){
        // 判断当前用户是否登录
        if(httpSession.getAttribute("u") == null){
            return ServerResponse.toFail(Const.UserEnum.NO_LOGIN.getCode(),Const.UserEnum.NO_LOGIN.getDesc());
        }
        return payinfoService.zf(shopName,orderNo,totalPrice,returnUrl);
    }


    @RequestMapping("returnPayPage")
    public void payPageReturn(HttpServletResponse httpResponse, String out_trade_no, String total_amount, String timestamp) throws IOException {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("订单支付成功！");
        stringBuffer.append("订单号：");
        stringBuffer.append(out_trade_no);
        stringBuffer.append("，金额：");
        stringBuffer.append(total_amount);
        stringBuffer.append("，支付时间：");
        stringBuffer.append(timestamp);

        httpResponse.setContentType( "text/html;charset="  + CHARSET);
        httpResponse.getWriter().write(stringBuffer.toString()); //直接将完整的表单html输出到页面
        httpResponse.getWriter().write("<br><a href='http://localhost:8089'>回到首页</a>"); //跳转回首页
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }

    @RequestMapping("yq")
    public void zfbYQ(HttpSession httpSession,HttpServletResponse response,HttpServletRequest request) throws IOException {
        // 获取支付宝请求参数集合
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 处理集合中数据，符合验签标准
        Map<String, String> parMap = new HashMap<>();
        for (String s : parameterMap.keySet()) {
            String[] strings = parameterMap.get(s);
            parMap.put(s,strings[0]);
        }
        // 验签
        boolean b = payinfoService.zfbYQ(parMap);
        // 验签结果
        if(b){
            // 执行业务操作
            response.getWriter().write("success");
        }else{
            response.getWriter().write("failure");
        }
    }
}

